"""
1、有这样的一个列表，深度不可知，列表里面还会嵌套列表，我想要把这个列表从左到右拼接成一个新的列表，且顺序不变
举例：[12,33,[11,22,33,[55,66,99]],[55,66]]
变成：[12, 33, 11, 22, 33, 55, 66, 99, 55, 66]
2、编写装饰器，为多个函数加上认证的功能（用户的账号密码）
 要求登录成功一次，后续的函数都无需输入用户名和密码
3、请实现一个装饰器，把函数的返回值+100然后返回
4、请实现一个装饰器，通过一次调用使函数重复执行5次
5、输入任意个字符串,分别实现从小到大输出和从大到小输出
6、接收n个数字，求这些参数数字的和
7、编写一个函数，实现数字的自增（从0开始，每调用一次，数字+1）
"""
# 1、有这样的一个列表，深度不可知，列表里面还会嵌套列表，我想要把这个列表从左到右拼接成一个新的列表，且顺序不变
# 举例：[12,33,[11,22,33,[55,66,99]],[55,66]]
# 变成：[12, 33, 11, 22, 33, 55, 66, 99, 55, 66]

"""
def f_list(list1):
    result = []
    for i in list1:
        if type(i) == list:
            result.extend(f_list(i))
        else:
            result.append(i)
    return result


old_list = [12, 33, [11, 22, 33, [55, 66, 99]], [55, 66]]
new_list = f_list(old_list)
print(new_list)
"""
# 2、编写装饰器，为多个函数加上认证的功能（用户的账号密码）
#  要求登录成功一次，后续的函数都无需输入用户名和密码
"""
result = False #默认是登录失败
user_default = {'user': None, 'pwd': None}
def login(func): #需要传入修饰的func
    def setup(user,pwd,*args):
        global result
        global user_default
        if user_default['user'] and user_default['pwd']:
            func(*args)
            print("免登录")
        else:
            if user == 'admin' and pwd == '123':
                result = True
                func(*args)
            else:
                result = False
        return result
    return setup

@login
def shooping(name,age,money):
    print(f"{name}去购物，他今年{age}岁，花了{money}钱")
shooping('admin','123','王长帅',19,20)
shooping('王长帅',19,20)
"""
# 第二种解法
"""
result = False
def login(func):
    def setup(*args):
        global result
        if result:
            print("免登录")
            func(*args)
            return result
        else:
            username = input("请输入账号")
            password = input("请输入密码")
            if password == '123456' and username == 'admin':
                print("登录成功！")
                func(*args)
                result = True
                return result
    return setup
@login
def eat(name,fen):
    print(f"{name}吃{fen}")


eat("王长帅","粪")
eat("王长帅","粪")
"""
# 3、请实现一个装饰器，把函数的返回值+100然后返回
"""
def return_sum(func):
    def sum_100():
        global num
        num += 100
        func()
        return num
    return sum_100

num = 0
@return_sum
def sum_1():
    print("计算结束")



print(sum_1())
print(sum_1())
# print(sum_1())
"""
# 4、请实现一个装饰器，通过一次调用使函数重复执行5次
"""
def repeat(func):
    def repeat_in(n):
        try:
            for i in range(n):
                func()
        except:
            print("输入不是数字")
    return  repeat_in

@repeat
def  output_fun():
    print("这是个装饰器")

output_fun("a")
"""

# 5、输入任意个字符串,分别实现从小到大输出和从大到小输出
# 有问题
"""
def compare(func):
    def str_len():
        str1 = func()
        print("从小到大排序")
        min_len = sorted(str1)
        for i in min_len:
            print(i, end="")
        print()
        print("从大到小排序")
        max_len = sorted(str1, reverse=True)
        for i in max_len:
            print(i, end="")

    return str_len


@compare
def compare_mm():
    input_strings = input("请输入多个字符串，用空格分隔：").split(" ")
    return input_strings
compare_mm()
"""
# ok
"""
def str_all(func):
    def str_new(*args):
        dict1 = {}
        for i in args:
            dict1[i] = len(i)
        result = sorted(dict1,reverse=True)
        func(*args)
        for i in args:
            dict1[i] = len(i)
        result1 = sorted(dict1)
        func(*args)
        print(result)
        print(result1)
    return str_new

@str_all
def str1(*args):
    print(f"你输入的字符串是{args}")


str1("asd", "asdasda","asda")

"""
"""
def paixu(func):
    def d(c):
        func(c)
        a = sorted(c, key=lambda x:len(x))
        print("从小到大输出:", a)
        b = sorted(c, key=len, reverse=True)
        print("从大到小输出:", b)
    return d
@paixu
def e(h):
    pass
list1=["asd","asdad","asdasdasa","asdasdadadada"]
e(list1)
"""
# 6、接收n个数字，求这些参数数字的和
# 定死输入
"""
# 函数
def sum_num(*args):
    print(sum(args))

sum_num(1,2,3)
# 解释器
def sum_num(func):
    def sum_num_r(*arg):
        func(*arg)
        return sum(arg)
    return sum_num_r

@sum_num
def sum_num_n(*arg):
    pass
print(sum_num_n(1, 2, 3,4))

"""

# 随机输入
""""
# 函数
def sum_num(n):
    result = 0
    for i in range(n):
        num = int(input("请示输入数字"))
        result += num
    print(result)
sum_num(4)
# 解释器
def sum_num(func):
    def sum_num_r(n):
        func(n)
        result = 0
        for i in range(n):
            num = int(input("请示输入数字"))
            result += num
        return result
    return sum_num_r
@sum_num
def sum_num_n(n):
    return  n
ss = sum_num_n(4)
print(ss)
"""
# 7、编写一个函数，实现数字的自增（从0开始，每调用一次，数字+1）
"""
count = 0
def create_incrementor():
    def increment():
        global count
        count += 1
        return count
    return increment
i  = create_incrementor()
print(i())
print(i())
print(i())
"""

